class jieba():
    def __init__(self):
        self.list = []
        self.max_element_len = 0
    """加载字典文件"""
    def load_dict(self,file):
        f = open(file,'r')#,encoding='UTF-8'
        while True:
            line = f.readline()
            if not line:
                break
            word = line[:-1]
            if word != '':
                self.list.append(word)
        f.close()
        while self.list[-1] == '':
            del self.list[-1]
    """打印加载的字典"""
    def print_dict(self):
        print('='*20+'字典数据'+'='*20)
        print(self.list)
        print('-'*48)
    """最大逆向匹配分词算法"""
    """
    参数：准备分词的句子，分词符，信息提取
    返回：根据字典进行分词的句子
    """
    def cut(self, text, single='/', main_info=True):
        self.max_element_len = 0
        for i in self.list:
            l = len(i)
            if self.max_element_len < l:
                self.max_element_len = l
        seg_text = ''
        candidate_str =''
        while text != '':
            candidate_str = text[-(self.max_element_len):]
            while candidate_str not in self.list:
                if len(candidate_str) == 1 or candidate_str == '':
                    break
                candidate_str = candidate_str[1:]
            text = text[:-(len(candidate_str))]
            seg_text = candidate_str+single+seg_text
        if main_info:
            b = seg_text[:-1].split(single)
            c = []
            for f in b:
                if f in self.list:
                    c.append(f)
            return c
        else:
            return seg_text[:-1]
    """在程序中动态添加字典里没有的词"""
    def add_word(self, word, add_to_txt=not True):
        if word not in self.list:
            self.list.append(word)
        if add_to_txt:
            f = open('dict.txt','w')#,encoding='UTF-8'
            for i in self.list:
                f.writelines(i)
                f.writelines('\n')
            f.close()
    """在程序中动态添加字典里没有的词"""
    def add_words(self, words, add_to_txt=not True):
        for word in words:
            if word not in self.list:
                self.list.append(word)
        if add_to_txt:
            f = open('dict.txt','w')#,encoding='UTF-8'
            for i in self.list:
                f.writelines(i)
                f.writelines('\n')
            f.close()
    """在程序中动态的删除字典中存在的词"""
    def del_word(self, word, add_to_txt=not True):
        if word in self.list:
            self.list.remove(word)
        if add_to_txt:
            with open('dict.txt','w') as f:#,encoding='UTF-8'
                for i in self.list:
                    f.writelines(i)
                    f.writelines('\n')
    """在程序中动态的删除字典中存在的词"""
    def del_words(self, words, add_to_txt=not True):
        for word in words:
            if word in self.list:
                self.list.remove(word)
        if add_to_txt:
            with open('dict.txt','w') as f:#,encoding='UTF-8'
                for i in self.list:
                    f.writelines(i)
                    f.writelines('\n')
if __name__ == '__main__':
    s = jieba()
    s.load_dict('dict.txt')
    s.add_words(['你好','世界'],True)
    s.print_dict()s
    s.del_words(['你好','世界'],True)
    s.print_dict()
    while True:
        text = input('请输入：')
        seg = s.cut(text)
        print(seg)
